#--------------------------------------------------------------------------#
#                                                                          #
#                                                                          #
#                                                                          #
#             Why does a nation choose to remain at civil war?             #
#                                                                          #
#                Submitted to: Small Wars and Insurgencies                 #
#                                                                          #
#                               APPENDIX                                   #
#                                 2022                                     #
#--------------------------------------------------------------------------#
# Working_directory 
#
setwd("C:/Why_does_a_nation_choose_to_remain_at_civil_war.dta")

#                                Packages                                  #
install.packages("plyr")
install.packages("gmodels")
install.packages("rio")
install.packages("piecewiseSEM")




#
#                          Active Libraries                                    #
library(dplyr)
library(plyr)
library(rio)
library(stargazer) 
library(jtools)
library(openxlsx)
library(aod)
library(ggplot2)



#                                 Variables                                    #

Yes_peace<-Is_peace_a_participatory_practice$Yes
No_peace<-Is_peace_a_participatory_practice$No
FARC<-Is_peace_a_participatory_practice$FARC
Paras<-Is_peace_a_participatory_practice$Paramilitaries
Yes_dummy<-Is_peace_a_participatory_practice$Yes_dummie
No_dummy<-Is_peace_a_participatory_practice$No_dummie
Voter_turnout<-Is_peace_a_participatory_practice$`Voters turnout`
Victims<-Is_peace_a_participatory_practice$Victims
Rural_poverty<-Is_peace_a_participatory_practice$Rural_Pov_
Coca<-Is_peace_a_participatory_practice$Coca_crops
Distance<-Is_peace_a_participatory_practice$Distance
Population<-Is_peace_a_participatory_practice$Popul
log(Population)
#                            Explore variables                                 #
par(mfrow=c(2,2))
plot(density(FARC))
plot(density(Paras))
plot(density(Yes_peace))
plot(density(No_peace))
plot(density(Voter_tunrout))


#                                Previous datasets                             #
Direct_consultation<-data.frame(FARC, Paras, Yes_peace, No_peace, Yes_dummy,
                                No_dummy, Voter_turnout, Victims, Rural_poverty,
                                Coca, Distance, Population)
Direct_consultation[is.na(Direct_consultation)] <- 0

#                         Appendix A correlations                              #

corrm<-cor(Direct_consultation)
Corr<-round(corrm, digits=3)
print(Corr)
export(Corr, "correlation_IPPP2.xlsx")

# Table . Summary statistics 
sum.tab<- subset(Direct_consultation, select=c(FARC, Paras, Yes_peace, No_peace,
                                               Voter_turnout,
                                               Victims, Rural_poverty, Coca, Distance,
                                               Population));
summary_stats<-summary(sum.tab)
summary_stats1<-print(summary_stats)
write.csv(summary_stats1, "summary_stats.csv")
sd(FARC, na.rm = FALSE)
sd(Paras, na.rm = FALSE)
sd(Yes_peace, na.rm = FALSE)
sd(No_peace, na.rm = FALSE)
sd(Voter_turnout, na.rm = FALSE)
sd(Victims, na.rm = FALSE)
sd(Rural_poverty, na.rm = FALSE)
sd(Coca, na.rm = FALSE)
sd(Distance, na.rm = FALSE)
sd(Population, na.rm = FALSE)
var(FARC, na.rm = FALSE)
var(Paras, na.rm = FALSE)
var(Yes_peace, na.rm = FALSE)
var(No_peace, na.rm = FALSE)
var(Voter_turnout, na.rm = FALSE)
var(Victims, na.rm = FALSE)
var(Rural_poverty, na.rm = FALSE)
var(Coca, na.rm = FALSE)
var(Distance, na.rm = FALSE)
var(Population, na.rm = FALSE)

#                                    Modeling                                  #

## OLS 

M.1_ols<-lm(Yes_peace~FARC+Paras+Voter_turnout+Victims+Rural_poverty+Coca+Distance+log(Population))
M.2_ols<-lm(No_peace~FARC+Paras+Voter_turnout+Victims+Rural_poverty+Coca+Distance+log(Population))
stargazer(M.1_ols, M.2_ols,  type = "text")
Modeling<-stargazer(M.1_ols, M.2_ols,  type = "text")
Modeling2<-data.frame(Modeling)
write.csv(Modeling,"models_direct_consultation.csv", row.names = FALSE)
RMSE<-function(error){ sqrt(mean(error^2)) }
RMSE(M.1_ols$residuals)
RMSE(M.2_ols$residuals)
#                        Models visualization                          #

plot_summs(M.1_ols, M.2_ols, scale = TRUE, plot.distributions = TRUE)
effect_plot(M.1_ols, pred = FARC, interval = TRUE, plot.points = TRUE)
effect_plot(M.1_ols, pred = Paras, interval = TRUE, plot.points = TRUE)
effect_plot(M.1_ols, pred = Voter_turnout, interval = TRUE, plot.points = TRUE)
effect_plot(M.1_ols, pred = Victims, interval = TRUE, plot.points = TRUE)
effect_plot(M.1_ols, pred = Rural_poverty, interval = TRUE, plot.points = TRUE)
effect_plot(M.1_ols, pred = Coca, interval = TRUE, plot.points = TRUE)
effect_plot(M.2_ols, pred = FARC, interval = TRUE, plot.points = TRUE) 
effect_plot(M.2_ols, pred = Paras, interval = TRUE, plot.points = TRUE)
effect_plot(M.2_ols, pred = Voter_turnout, interval = TRUE, plot.points = TRUE)
effect_plot(M.2_ols, pred = Victims, interval = TRUE, plot.points = TRUE)
effect_plot(M.2_ols, pred = Rural_poverty, interval = TRUE, plot.points = TRUE)
effect_plot(M.2_ols, pred = Coca, interval = TRUE, plot.points = TRUE)

# -------------------------------------------------------------------- #

#                          Logit regression                            #
str(Direct_consultation)
FARCF<-as.factor(FARC)
ParasF<-as.factor(Paras)
Direct_consul_Logit<-data.frame(Yes_dummy, No_dummy, FARCF, ParasF, Voter_turnout,
                                Victims, Rural_poverty, Coca, Distance, Population)
Logit_peace<-glm(formula = Yes_dummy ~ FARCF + ParasF + Voter_turnout + Victims + 
                   Rural_poverty + Coca + Distance + log(Population), family = "binomial")
Logit_peace2<-glm(formula = No_dummy ~ FARCF + ParasF + Voter_turnout + Victims + 
                   Rural_poverty + Coca + Distance + log(Population), family = "binomial")
Logit_peace3<-glm(formula = Yes_dummy ~ FARC + Paras + Voter_turnout + Victims + 
                    Rural_poverty + Coca + Distance + log(Population), family = "binomial")
Logit_peace4<-glm(formula = No_dummy ~ FARC + Paras + Voter_turnout + Victims + 
                    Rural_poverty + Coca + Distance + log(Population), family = "binomial")

Logit_models<- stargazer(Logit_peace, Logit_peace2, Logit_peace3, Logit_peace4, type = "text")
write.csv(Logit_models,"Logit-models_direct_consultation.csv", row.names = FALSE)

## Now calculate the overall "Pseudo R-squared" and its p-value

ll.null_Logit1 <- Logit_peace$null.deviance/-2
ll.proposed_Logit1<- Logit_peace$deviance/-2
ll.null_Logit2<-Logit_peace2$null.deviance/-2
ll.proposed_Logit2<- Logit_peace2$deviance/-2
ll.null_Logit3 <- Logit_peace3$null.deviance/-2
ll.proposed_Logit3<- Logit_peace3$deviance/-2
ll.null_Logit4 <- Logit_peace4$null.deviance/-2
ll.proposed_Logit4<- Logit_peace4$deviance/-2

## McFadden's Pseudo R^2 = [ LL(Null) - LL(Proposed) ] / LL(Null)
(ll.null_Logit1 - ll.proposed_Logit1) / ll.null_Logit1
(ll.null_Logit2 - ll.proposed_Logit2) / ll.null_Logit2
(ll.null_Logit3 - ll.proposed_Logit3) / ll.null_Logit3
(ll.null_Logit4 - ll.proposed_Logit4) / ll.null_Logit4

## The p-value for the R^2
1 - pchisq(2*(ll.proposed_Logit1 - ll.null_Logit1), df=(length(Logit_peace$coefficients)-1))
2 - pchisq(2*(ll.proposed_Logit2 - ll.null_Logit2), df=(length(Logit_peace2$coefficients)-1))

